# Copyright (C) 2009,2010,2011,2012  Marco Restelli
#
# This file is part of:
#   FEMilaro -- Finite Element Method toolkit
#
# FEMilaro is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# FEMilaro is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with FEMilaro; If not, see <http://www.gnu.org/licenses/>.
#
# authors: 
#         Marco Restelli                   <marco.restelli@gmail.com>
#         Carlo de Falco

# List of the object files of the present folder
OBJ= mod_eigen.o \
     mod_fu_manager.o \
     mod_interp1d.o \
     mod_kinds.o \
     mod_linal.o \
     mod_messages.o \
     mod_numquad.o \
     mod_octave_io.o \
     mod_octave_io_perms.o \
     mod_octave_io_sparse.o \
     mod_octave_io_sympoly.o \
     mod_output_control.o \
     mod_perms.o \
     mod_sparse.o \
     mod_state_vars.o \
     mod_symmon.o \
     mod_sympoly.o \
     mod_utils.o \
     $(MOD_OMP_UTILS) \
     $(MOD_MPI_UTILS).o
# These are the modules that are meant to be USEd by the library users
LIBRARY_MOD_FILES= $(OBJ:.o=.mod)

ifeq ($(USING_OCTAVE), Yes)
     OCTOBJ=mod_octave_io_binary.o \
            octave_file_io.o 
endif

OBJ_EIGENTEST= eigen_test.o \
     mod_utils.o \
     mod_messages.o \
     mod_kinds.o \
     mod_eigen.o

# General settings -----------------------------------

PWD:=$(shell pwd)
BUILDDIR:=$(BUILDDIR)/fml_general_utilities

# VPATH includes:
#   PWD for the source files (included by default)
#   BUILDDIR for the .o prerequisites
#   LIBDIR for the final library
VPATH:=$(BUILDDIR) $(LIBDIR)

# clear unfinished targets
.DELETE_ON_ERROR:
# define the phony targets
.PHONY: clean

# check waht to do with OpenMP
ifeq ($(USING_OMP),Yes)
  MOD_OMP_UTILS=mod_omp_utils.o
else
  MOD_OMP_UTILS=
endif

# check waht to do with MPI
ifeq ($(USING_MPI),Yes)
  MOD_MPI_UTILS=mod_mpi_utils
  # remove the unused version of the module
  XXX:=$(shell $(RM) $(BUILDDIR)/mod_mpi_utils_dummy.o)
else
  MOD_MPI_UTILS=mod_mpi_utils_dummy
  # we have to be careful how to compile the dummy module
  ifeq ($(FC_BASE),ifort)
    FC_MINIMAL=$(FC) -assume std_mod_proc_name
  else
    FC_MINIMAL=$(FC)
  endif
  # remove the unused version of the module
  XXX:=$(shell $(RM) $(BUILDDIR)/mod_mpi_utils.o)
endif

# Main targets ---------------------------------------

libfml_general_utilities.a: $(OBJ) $(OCTOBJ)
	cd $(BUILDDIR) && \
	  $(AR) -r $(LIBDIR)/libfml_general_utilities.a \
	  $(OBJ) $(OCTOBJ)
	for mod in $(LIBRARY_MOD_FILES);     \
        do                                   \
          cp $(BUILDDIR)/$${mod} $(LIBDIR)/; \
        done

eigen-test: $(OBJ_EIGENTEST)
	cd $(BUILDDIR) && \
	  $(LD) $(LDFLAGS) $(OBJ_EIGENTEST) -o $(BINDIR)/eigen-test

# General rule
%.o: %.f90
	cd $(BUILDDIR) && $(FC) $(FFLAGS) -c $(PWD)/$<

clean:
	$(RM) $(BUILDDIR)/*.o $(BUILDDIR)/*.ilo $(BUILDDIR)/*.mod \
          $(LIBDIR)/libfml_general_utilities.a
	for mod in $(LIBRARY_MOD_FILES); \
        do                               \
          $(RM) $(LIBDIR)/$${mod};       \
        done

# Specific dependencies: because of USE directives ---

octave_file_io.o: octave_file_io.cc octave_file_io.h
	cd $(BUILDDIR) && $(MKOCTFILE) -I$(PWD) -c $(PWD)/$<

# We have to filter some "pedantic" compiler flags to compile this
# module: add all the flags that must be filtered on the corresponding
# line as a space separated list.
mod_mpi_utils.o: mod_mpi_utils.f90 \
  mod_messages.o \
  mod_kinds.o
	cd $(BUILDDIR) && $(FC) $(filter-out \
	  -std=f2003 \
	 , $(FFLAGS)) -c $(PWD)/$<

# This module requires many tricks, so we have to switch off
# debugging. Also, we have to hide the .o while compiling the other
# modules.
mod_mpi_utils_dummy.o: mod_mpi_utils_dummy.f90 \
  mod_messages.o \
  mod_kinds.o
	cd $(BUILDDIR) && $(FC_MINIMAL) -O0 -c $(PWD)/$<


mod_eigen.o: \
  mod_messages.o \
  mod_kinds.o

mod_fu_manager.o: \
  mod_messages.o

mod_interp1d.o: \
  mod_messages.o \
  mod_kinds.o

mod_kinds.o: \
  mod_messages.o

mod_linal.o: \
  mod_messages.o \
  mod_kinds.o \
  mod_eigen.o

mod_messages.o: \
  mod_utils.o

mod_numquad.o: \
  mod_messages.o \
  mod_kinds.o \
  mod_fu_manager.o \
  mod_octave_io.o \
  mod_linal.o \
  mod_perms.o

mod_octave_io.o: \
  mod_messages.o \
  mod_kinds.o

mod_octave_io_perms.o: \
  mod_messages.o \
  mod_kinds.o \
  mod_octave_io.o \
  mod_perms.o

mod_octave_io_sparse.o: \
  mod_messages.o \
  mod_kinds.o \
  mod_octave_io.o \
  mod_sparse.o

mod_octave_io_sympoly.o: \
  mod_messages.o \
  mod_kinds.o \
  mod_octave_io.o \
  mod_sympoly.o

momod_output_control.o: \
  mod_messages.o \
  mod_kinds.o

mod_perms.o: \
  mod_messages.o \
  mod_linal.o

mod_sparse.o: \
  mod_messages.o \
  mod_kinds.o \
  $(MOD_OMP_UTILS)

mod_state_vars.o: \
  mod_messages.o \
  mod_kinds.o

mod_symmon.o: \
  mod_messages.o \
  mod_kinds.o

mod_sympoly.o: \
  mod_messages.o \
  mod_kinds.o \
  mod_symmon.o

mod_omp_utils.o: \
  mod_utils.o

eigen_test.o: \
  mod_messages.o \
  mod_kinds.o \
  mod_eigen.o

